<template>
  <div class="dashboard-container">
    <h2>仪表盘</h2>
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card class="stat-card">
          <template #header>
            <div class="card-header">
              <span>社团总数</span>
            </div>
          </template>
          <transition name="number-fade" mode="out-in">
            <div :key="stats.clubCount" class="stat-number">{{ stats.clubCount }}</div>
          </transition>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card">
          <template #header>
            <div class="card-header">
              <span>活动总数</span>
            </div>
          </template>
          <transition name="number-fade" mode="out-in">
            <div :key="stats.activityCount" class="stat-number">{{ stats.activityCount }}</div>
          </transition>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card">
          <template #header>
            <div class="card-header">
              <span>新闻总数</span>
            </div>
          </template>
          <transition name="number-fade" mode="out-in">
            <div :key="stats.newsCount" class="stat-number">{{ stats.newsCount }}</div>
          </transition>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card">
          <template #header>
            <div class="card-header">
              <span>用户总数</span>
            </div>
          </template>
          <transition name="number-fade" mode="out-in">
            <div :key="stats.userCount" class="stat-number">{{ stats.userCount }}</div>
          </transition>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import axios from 'axios'

const stats = ref({
  clubCount: 0,
  activityCount: 0,
  newsCount: 0,
  userCount: 0
})

const loadClubCount = async () => {
  try {
    const res = await axios.get('/club/totalCount')
    if (res.data.status === 'success') {
      stats.value.clubCount = res.data.data
    }
  } catch (error) {
    console.error('获取社团总数失败:', error)
  }
}

const loadActivityCount = async () => {
  try {
    const res = await axios.get('/activity/totalCount')
    if (res.data.status === 'success') {
      stats.value.activityCount = res.data.data
    }
  } catch (error) {
    console.error('获取活动总数失败:', error)
  }
}

const loadNewsCount = async () => {
  try {
    const res = await axios.get('/news/totalCount')
    if (res.data.status === 'success') {
      stats.value.newsCount = res.data.data
    }
  } catch (error) {
    console.error('获取新闻总数失败:', error)
  }
}

const loadUserCount = async () => {
  try {
    const res = await axios.get('/user/totalCount')
    if (res.data.status === 'success') {
      stats.value.userCount = res.data.data
    }
  } catch (error) {
    console.error('获取用户总数失败:', error)
  }
}

onMounted(() => {
  loadClubCount()
  loadActivityCount()
  loadNewsCount()
  loadUserCount()
})

// 添加过渡效果的CSS样式
</script>

<style scoped>
.dashboard-container {
  padding: 20px;
}

.stat-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  color: #409EFF;
  text-align: center;
  padding: 20px 0;
}

/* 添加过渡效果的CSS样式 */
.number-fade-enter-active,
.number-fade-leave-active {
  transition: all 0.5s ease;
}

.number-fade-enter-from,
.number-fade-leave-to {
  opacity: 0;
  transform: translateY(-10px);
}
</style>